package service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import model.Tgood;
import model.Tgoodtypes;
import model.Tinventory;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import pageModel.DataGrid;
import pageModel.Good;
import dao.GoodDaoI;
import dao.GoodTypesDaoI;
import dao.InventoryDaoI;
import service.GoodServiceI;

@Service("goodService")
public class GoodServiceImpl implements GoodServiceI {

private GoodDaoI goodDao;
private InventoryDaoI inventoryDao;
private GoodTypesDaoI goodTypesDao;
public GoodDaoI getGoodDao() {
	return goodDao;
}
@Autowired
public void setGoodDao(GoodDaoI goodDao) {
	this.goodDao = goodDao;
}

public InventoryDaoI getInventoryDao() {
	return inventoryDao;
}
@Autowired
public void setInventoryDao(InventoryDaoI inventoryDao) {
	this.inventoryDao = inventoryDao;
}

public GoodTypesDaoI getGoodTypesDao() {
	return goodTypesDao;
}
@Autowired
public void setGoodTypesDao(GoodTypesDaoI goodTypesDao) {
	this.goodTypesDao = goodTypesDao;
}
@Override
public DataGrid datagrid(Good good) {
	DataGrid dg=new DataGrid();
	String hql="from Tgood t where 1=1";
	Map<String,Object> params=new HashMap<String,Object>();
	hql = addWhere(good, hql, params);
	String totalHql="select count(*) "+hql;
	if(good.getSort()!=null){
	hql+=" order by "+good.getSort()+" "+good.getOrder();
	}
	List<Tgood> su=goodDao.find(hql,params,good.getPage(),good.getRows());
	/*System.out.println(su);*/
	List<Good> l=new ArrayList<Good>();
	if(su!=null&&su.size()>0){
		for(Tgood t :su){
			Good u=new Good();
			BeanUtils.copyProperties(t, u);
			if(t.getTgoodtypes()!=null){
				u.setTypeid(t.getTgoodtypes().getId());
				u.setType(t.getTgoodtypes().getName());
				u.setTgoodtypes(t.getTgoodtypes());
			}
			l.add(u);
		}
	}
	dg.setTotal(goodDao.count(totalHql,params));
	dg.setRows(l);
	return dg;
}
private String addWhere(Good good, String hql, Map<String, Object> params) {
	if(good.getCode()!=null&&!good.getCode().trim().equals("")){
	hql+=" and t.code like :code";
	params.put("code", "%%"+good.getCode()+"%%");
	}
	if(good.getName()!=null&&!good.getName().trim().equals("")){
		hql+=" and t.name like :name";
		params.put("name", "%%"+good.getName()+"%%");
		}
	return hql;
}
public Good save(Good good){
	   Tgood u=new  Tgood();
	   Tgoodtypes tg=new Tgoodtypes();
	   BeanUtils.copyProperties(good,u);
	   u.setId(UUID.randomUUID().toString());
	   /*System.out.print(good.getTypeid());*/
	   tg.setId(good.getTypeid());
	   u.setTgoodtypes(tg);
	   goodDao.save(u);
	   BeanUtils.copyProperties(u, good);
	   Tinventory i=new Tinventory();
	   i.setId(UUID.randomUUID().toString());
	   i.setTgood(u);
	   i.setOriginalsum(50);
	   i.setAmount(50);
	   inventoryDao.save(i);
	   return good;
}
public void edit(Good good) {
	Tgood t=goodDao.get(Tgood.class, good.getId());
	String hql="from Tgoodtypes t where t.id=:id or t.name=:name";
	Map<String,Object> params=new HashMap<String,Object>();
	params.put("id", good.getTypeid());
	params.put("name", good.getTypeid());
	Tgoodtypes tg = goodTypesDao.get(hql,params);
	BeanUtils.copyProperties(good, t);
	t.setTgoodtypes(tg);
	BeanUtils.copyProperties(t, good);
} 
public void remove(String ids) {
	String [] nids=ids.split(",");
	String hql="delete Tgood t where t.id in(";
	for(int i=0;i<nids.length;i++){
		if(i>0){
			hql+=",";
		}
		hql+="'"+nids[i]+"'";
	}
	hql+=")";
	goodDao.executeHql(hql);
	}
}
